﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using HNG.LIB.Controls;
public partial class AdminPortal_Controls_Menu_Menu_Info : System.Web.UI.UserControl
{    
    TblMenuManager _TblMenuManager = new TblMenuManager();
    int MId = 0;
    string LangID = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { MId = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { MId = 0; }

            FunctionExtend.LoadPositionMenu(ddlPoistionMenu);

            FunctionExtend.LoadShowType(ddlShowType);

            FunctionExtend.LoadTarget(ddlTarget);

            loadCateParent();

            if (MId == 0) //Add cate
            {
                lblTitle.Text = "Thêm Menu";
                Toolbox1.ShowBack = Toolbox1.ShowRefesh = Toolbox1.ShowDel = false;
            }
            else //sua cate
            {
                lblTitle.Text = "Sửa file: " + MId;
                BindData(MId);
            }
        }
    }

    protected void BindData(int ID)
    {
        TblMenuEntity _TblMenuEntity = _TblMenuManager.SelectOne(ID);
        if (_TblMenuEntity == null) { lblthongbao.Text = "<img src='../../Images/validatebox_warning.png' alt='Cảnh báo' width='16' height='16' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        hidID.Value = ID.ToString();
        txtID.Text = ID.ToString();
        txtTitle.Text = _TblMenuEntity.TieuDe;
        txtMota.Text = Server.HtmlDecode(_TblMenuEntity.MoTa);
        ddlPoistionMenu.SelectedValue = _TblMenuEntity.ViTri.ToString();
        ddlCateParent.SelectedValue = _TblMenuEntity.ParentId.ToString();
        ddlShowType.SelectedValue = _TblMenuEntity.ShowType.ToString();
        ddlShowType_SelectedIndexChanged(new object(), new EventArgs());
        if (ddlShowType.SelectedValue == enumShowType._default.ToString())
            ListDanhMuc.SelectedValue = _TblMenuEntity.Url;
        else
        {
            if (!string.IsNullOrEmpty(_TblMenuEntity.DanhMucDetailId.ToString()))
            {
                chkShowByDetail.Checked = true;                                
                ListDanhMuc.SelectedValue = _TblMenuEntity.DanhMucId.ToString();
                chkShowByDetail_CheckedChanged(new object(), new EventArgs());
                ListDanhMuc_SelectedIndexChanged(new object(), new EventArgs());
                ListDataDetail.SelectedValue = _TblMenuEntity.DanhMucDetailId.ToString();
            }
            else
            {
                if (!string.IsNullOrEmpty(_TblMenuEntity.DanhMucId.ToString()))
                    ListDanhMuc.SelectedValue = _TblMenuEntity.DanhMucId.ToString();
            }
        }

        ddlTarget.SelectedValue = _TblMenuEntity.Target.ToString();
        txtOrder.Text = _TblMenuEntity.ThuTu.ToString();
        chkStatus.Checked = Convert.ToBoolean(_TblMenuEntity.TrangThai);
        //--luu lai DanhMucID
        hidDanhMuc.Value = _TblMenuEntity.DanhMucId.ToString();       
    }

    private void loadCateParent()
    {
        DataTable _dtDM = new DataTable();
        try
        {
            TblMenuEntity _TblMenuEntity = new TblMenuEntity();
            _TblMenuEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
            _TblMenuEntity.SortDirection = "ASC";
            _dtDM = _TblMenuManager.be_SelectToDropdownlist(_TblMenuEntity);
        }
        catch { _dtDM = null; }

        if (_dtDM != null)
            HNG.LIB.BIND_SOURCE.Bind_DropDownListV3(ddlCateParent, _dtDM, TblMenuFields.TieuDe.Name, TblMenuFields.Id.Name, TblMenuFields.ParentId.Name);
    }    

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblMenuManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/MenuEdit.aspx?id=") + hidID.Value);
        //BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {
        if (txtTitle.Text == "") { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Nhập tên menu !</span>"; txtTitle.Focus(); return; }
        TblMenuEntity _TblMenuEntity = new TblMenuEntity();
        if (!string.IsNullOrEmpty(hidID.Value)) _TblMenuEntity.Id = Convert.ToInt32(hidID.Value);//update
        _TblMenuEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblMenuEntity.TieuDe = txtTitle.Text;
        _TblMenuEntity.MoTa = Server.HtmlEncode(txtMota.Text);
        _TblMenuEntity.ViTri = ddlPoistionMenu.SelectedValue;
        _TblMenuEntity.ParentId = Convert.ToInt32( ddlCateParent.SelectedValue);
        _TblMenuEntity.ShowType = ddlShowType.SelectedValue;
        if (!string.IsNullOrEmpty(txtOrder.Text))
            _TblMenuEntity.ThuTu = Convert.ToInt32(txtOrder.Text);
        _TblMenuEntity.TrangThai = (chkStatus.Checked == true) ? true : false;
        _TblMenuEntity.Target = ddlTarget.SelectedValue;
        _TblMenuEntity.TenDanhMucCha = ddlCateParent.SelectedItem.Text;
        if (ddlShowType.SelectedValue == enumShowType._default.ToString())
        {
            _TblMenuEntity.Url = ListDanhMuc.SelectedValue;
        }
        else
        {
            if (chkShowByDetail.Checked) //neu chon menu luu 1 tin chi tiet
            {
                if (ListDataDetail.Items.Count > 0)
                {
                    _TblMenuEntity.DanhMucId = Convert.ToInt32(ListDanhMuc.SelectedValue);
                    _TblMenuEntity.DanhMucDetailId = Convert.ToInt32(ListDataDetail.SelectedValue);
                    if (ddlShowType.SelectedValue == enumShowType._DanhMucSanPham.ToString())
                        _TblMenuEntity.Url = string.Format(HNG.LIB.Common.Connection.GetSetting("ProductDetailUrl"), LangID, ListDanhMuc.SelectedValue, _TblMenuEntity.DanhMucDetailId, 1, HNG.LIB.Common.FuncCheck.CheckTitle(ListDataDetail.SelectedItem.Text));
                    else
                        _TblMenuEntity.Url = string.Format(HNG.LIB.Common.Connection.GetSetting("NewsDetailUrl"), LangID, _TblMenuEntity.DanhMucDetailId, HNG.LIB.Common.FuncCheck.CheckTitle(ListDataDetail.SelectedItem.Text));
                }
                else
                {
                    _TblMenuEntity.DanhMucId = Convert.ToInt32(ListDanhMuc.SelectedValue);
                    if (ddlShowType.SelectedValue == enumShowType._DanhMucSanPham.ToString())
                        _TblMenuEntity.Url = string.Format(HNG.LIB.Common.Connection.GetSetting("ProductListByCate"), LangID, _TblMenuEntity.DanhMucId, 1);
                    else
                        _TblMenuEntity.Url = string.Format(HNG.LIB.Common.Connection.GetSetting("NewsListByCate"), LangID, _TblMenuEntity.DanhMucId, 1);
                    _TblMenuEntity.DanhMucDetailId = null;
                }
            }
            else
            {
                if (ListDanhMuc.Items.Count > 0)
                {
                    _TblMenuEntity.DanhMucId = Convert.ToInt32(ListDanhMuc.SelectedValue);
                    if (ddlShowType.SelectedValue == enumShowType._DanhMucSanPham.ToString())
                        _TblMenuEntity.Url = string.Format(HNG.LIB.Common.Connection.GetSetting("ProductListByCate"), LangID, _TblMenuEntity.DanhMucId, 1);
                    else
                        _TblMenuEntity.Url = string.Format(HNG.LIB.Common.Connection.GetSetting("NewsListByCate"), LangID, _TblMenuEntity.DanhMucId, 1);
                    _TblMenuEntity.DanhMucDetailId = null;
                }
            }
        }
        
        if (!string.IsNullOrEmpty(hidID.Value))
            _TblMenuManager.Update(_TblMenuEntity);//update
        else
        {
            _TblMenuManager.Insert(_TblMenuEntity);//Insert
            hidID.Value = _TblMenuEntity.Id.ToString();
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void ddlShowType_SelectedIndexChanged(object sender, EventArgs e)
    {       
        if (ddlShowType.SelectedValue == enumShowType._DanhMucSanPham.ToString())
        {
            loadDanhMucSanPham();
            checkDetail.Visible  = true;
            chkShowByDetail.Checked = ShowDataDetail.Visible = false;
            chkShowByDetail_CheckedChanged(new object(), new EventArgs());
            try { ListDanhMuc.SelectedValue = hidDanhMuc.Value; }
            catch { }
        }
        else if (ddlShowType.SelectedValue == enumShowType._DanhMucTin.ToString())
        {
            loadDanhMucTin();
            checkDetail.Visible  = true;
            chkShowByDetail.Checked = ShowDataDetail.Visible = false;
            chkShowByDetail_CheckedChanged(new object(), new EventArgs());
            try { ListDanhMuc.SelectedValue = hidDanhMuc.Value; }
            catch { }
        }
        else if (ddlShowType.SelectedValue == enumShowType._default.ToString())
        {
            FunctionExtend.LoadShowTypeDefault(ListDanhMuc);
            checkDetail.Visible = ShowDataDetail.Visible = false;
            ListDanhMuc.AutoPostBack = false;
        }else
        {
            ListDanhMuc.Items.Clear();
            checkDetail.Visible = ShowDataDetail.Visible = false;
            ListDanhMuc.AutoPostBack = false;
        }
    }

    private void loadDanhMucSanPham()
    {
        ListDanhMuc.Items.Clear();
        DataTable _dtDM = new DataTable();
        try
        {
            TblDanhMucSanPhamEntity _TblDanhMucSanPhamEntity = new TblDanhMucSanPhamEntity();
            _TblDanhMucSanPhamEntity.LangId = LangID;
            _dtDM = new TblDanhMucSanPhamManager().be_SelectToDropdownlist(_TblDanhMucSanPhamEntity);
        }
        catch { _dtDM = null; }

        if (_dtDM != null || _dtDM.Rows.Count > 0)
            HNG.LIB.BIND_SOURCE.Bind_DataListBox(ListDanhMuc, _dtDM, TblDanhMucSanPhamFields.TenDanhMuc.Name, TblDanhMucSanPhamFields.Id.Name, TblDanhMucSanPhamFields.DanhMucCha.Name);
    }

    private void loadDanhMucTin()
    {
        ListDanhMuc.Items.Clear();
        DataTable _dtDM = new DataTable();
        try
        {
            TblDanhMucTinEntity _TblDanhMucTinEntity = new TblDanhMucTinEntity();
            _TblDanhMucTinEntity.LangId = LangID;
            _TblDanhMucTinEntity.Type = "category";
            _dtDM = new TblDanhMucTinManager().be_SelectToDropdownlist(_TblDanhMucTinEntity);
        }
        catch { _dtDM = null; }

        if (_dtDM != null || _dtDM.Rows.Count > 0)
            HNG.LIB.BIND_SOURCE.Bind_DataListBox(ListDanhMuc, _dtDM, TblDanhMucTinFields.TenDanhMuc.Name, TblDanhMucTinFields.Id.Name, TblDanhMucTinFields.DanhMucCha.Name);
    }
    protected void chkShowByDetail_CheckedChanged(object sender, EventArgs e)
    {
        if (chkShowByDetail.Checked)
        {
            ShowDataDetail.Visible = true;
            ListDanhMuc.AutoPostBack = true;
        }
        else
        {
            ShowDataDetail.Visible = false;
            ListDataDetail.Items.Clear();
            ListDanhMuc.AutoPostBack = false;
        }
    }

    //load tin theo DanhmucsanphamID
    private void LoadChitietSanPham(int DanhMucSanPHamID) {
        ListDataDetail.Items.Clear();
        try{
            DataTable _dt = new TblSanPhamManager().be_selectToDropdownlist(DanhMucSanPHamID);
            if (_dt != null)
            {
                foreach (DataRow r in _dt.Rows)
                {
                    ListItem item = new ListItem(Server.HtmlDecode(r[TblSanPhamFields.TenSanPham.Name].ToString()), r[TblSanPhamFields.Id.Name].ToString());
                    ListDataDetail.Items.Add(item);
                }
            }
        }catch{}
    }

    //load tin theo DanhmuctinID
    private void LoadChitietTin(int DanhMucTinID)
    {
        ListDataDetail.Items.Clear();
        try
        {
            DataTable _dt = new TblTinTucManager().be_selectToDropdownlist(DanhMucTinID);
            if (_dt != null)
            {
                foreach (DataRow r in _dt.Rows)
                {
                    ListItem item = new ListItem(Server.HtmlDecode(r[TblTinTucFields.TieuDe.Name].ToString()), r[TblTinTucFields.Id.Name].ToString());
                    ListDataDetail.Items.Add(item);
                }
            }
        }
        catch { }
    }
    protected void ListDanhMuc_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlShowType.SelectedValue == enumShowType._DanhMucSanPham.ToString())
        {
            LoadChitietSanPham(Convert.ToInt32(ListDanhMuc.SelectedValue));
        }
        else if (ddlShowType.SelectedValue == enumShowType._DanhMucTin.ToString())
        {
            LoadChitietTin(Convert.ToInt32(ListDanhMuc.SelectedValue));
        }       
    }
}